package com.yangfei.mystudy1020.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.yangfei.mystudy1020.R;
import com.yangfei.mystudy1020.db.YFResponse;
import com.yangfei.mystudy1020.db.phone;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;

public class RetrofitActivity extends Activity {
    static String TAG = RetrofitActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);

        new Thread(() -> {
            try {
                doyfTest();
                doyfTestRest();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

    }

    public static final String PHP_API_URL = "http://127.0.0.1:8081";

    public interface yftestPhp {
        @GET("/phpapp/yf_php_study/DBHandler.php")
        Call<YFResponse> data();
    }

    public void doyfTest() throws IOException {
        // Create a very simple REST adapter which points the GitHub API.
        Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(PHP_API_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

        // Create an instance of our GitHub API interface.
        yftestPhp yftest = retrofit.create(yftestPhp.class);

        // Create a call instance for looking up Retrofit contributors.
        Call<YFResponse> call = yftest.data();
        for (YFResponse.DataDTO data : call.execute().body().getData()) {
            Log.d(TAG, data.getFirstname() + data.getLastname());
        }
    }

    public interface yftestPhpRest {
        @GET("/phpapp/restexample/mobile/list/")
        Call<List<phone>> data();
    }

    public void doyfTestRest() throws IOException {
        // Create a very simple REST adapter which points the GitHub API.
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(PHP_API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // Create an instance of our GitHub API interface.
        yftestPhpRest yftest = retrofit.create(yftestPhpRest.class);

        // Create a call instance for looking up Retrofit contributors.
        Call<List<phone>> pList = yftest.data();
        for (phone data : pList.execute().body()) {
            Log.d(TAG, data.getName() + data.getIndex());
        }
    }
}